package com.h.heal.controller;

import com.h.heal.common.MyPageInfo;
import com.h.heal.entity.Video;
import com.h.heal.mapper.VideoMapper;
import com.h.heal.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/video")
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private VideoMapper videoMapper;

    @RequestMapping("/main")
    public ModelAndView main(HttpServletRequest request, ModelAndView mv) {
        mv.setViewName("/admin/video/main");
        MyPageInfo myPageInfo = videoService.findBySearch(request);
        mv.addObject("pageInfo", myPageInfo);
        return mv;
    }

    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request, ModelAndView mv) {
        mv.setViewName("/user/video/index");
        List<Video> video = videoService.findAll();
        mv.addObject("list",video);
        return mv;
    }


    @RequestMapping("/user/main")
    public ModelAndView userMain(HttpServletRequest request, ModelAndView mv) {
        mv.setViewName("/user/video/main");

        MyPageInfo myPageInfo = videoService.findBySearchUser(request);
        mv.addObject("pageInfo", myPageInfo);
        return mv;
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id) {
        videoService.deleteById(id);
        return "redirect:/video/main";
    }

    @RequestMapping("/user/delete/{id}")
    public String userDelete(@PathVariable("id") int id) {
        videoService.deleteById(id);
        return "redirect:/video/user/main";
    }

    @RequestMapping("/creat")
    @ResponseBody
    public String creat(Video video) {

        return videoService.create(video);
    }


    @RequestMapping("/user/creat")
    @ResponseBody
    public String userCreat(Video video) {

        return videoService.create(video);
    }

    @RequestMapping("/openEdit/{id}")
    public ModelAndView openEdit(@PathVariable("id") int id, ModelAndView mv) {
        Video video = videoService.findById(id);
        mv.setViewName("/admin/video/edit");
        mv.addObject("video", video);
        return mv;
    }

    @RequestMapping("/doEdit")
    @ResponseBody
    public String doEdit(Video video) {
        return videoService.edit(video);
    }


    @RequestMapping("/user/openEdit/{id}")
    public ModelAndView userOpenEdit(@PathVariable("id") int id, ModelAndView mv) {
        Video video = videoService.findById(id);
        mv.setViewName("/user/video/edit");
        mv.addObject("video", video);
        return mv;
    }

    @RequestMapping("/user/doEdit")
    @ResponseBody
    public String userDoEdit(Video video) {
        return videoService.edit(video);
    }


    @RequestMapping("/like")
    @ResponseBody
    public String like(String id) {
        videoMapper.like(id);
        return "ok";  //重定向到登录页面

    }


    @RequestMapping("/vv")
    @ResponseBody
    public String vv(String id) {
        videoMapper.vv(id);
        return "ok";  //重定向到登录页面

    }
}
